package dao;

import hbt.HibernateUtil;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class HibernateDAO 
{
	protected static HibernateDAO instancia = null;
	protected static SessionFactory sf = null;
	protected static Session session = null;

	
	public static HibernateDAO getInstancia()
	{
		if(instancia == null)
		{
			instancia = new HibernateDAO();
		} 
		return instancia;
	}
	
	protected HibernateDAO()  
	{
		sf = HibernateUtil.getSessionFactory();
	}

	protected Session getSession()
	{
		if(session == null || !session.isOpen())
		{
			session = sf.openSession();
		}
		
		return session;
	}
	
	public void closeSession()
	{
		if (session.isOpen()) 
		{
			session.close();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public Object get (Class c, Object o) 
	{
		Session session = getSession();
		return session.get(c, (Serializable) o);
	}

	@SuppressWarnings("unchecked")
	public List<?> getLista(String query) 
	{
		try
		{
			Session session = getSession();
			List<?> lista = session.createQuery(query).list();
			return (List<Integer>)lista;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	public List<?> parametro(String query, String prmName, String prm) 
	{
		Session session = getSession();
		List<?> lista = session.createQuery(query).setParameter(prmName, prm).list();
		return lista;
	}
	
	public List<?> parametros(String query, List<String> prm, String prmName) 
	{
		try
		{
			Session session = getSession();
			List<?> lista = session.createQuery(query).setParameterList(prmName, prm).list();
			return lista;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return prm;
	}
	
	public Object propiedades(String query, Object obj) 
	{
		Session session = getSession();
		Object object = session.createQuery(query).setProperties(obj).uniqueResult();
		return object;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> tuplas(String query) 
	{
		Session session = getSession();
		List<Object[]> lista = session.createQuery(query).list();
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> tuplasNamedQuery(String nameQuery) 
	{
		try
		{
			Session session = getSession();
			List<Object[]> lista = session.getNamedQuery(nameQuery).list();
			return lista;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
		
	public Object totalizador(String query) 
	{
		Session session = getSession();
		Object obj = session.createQuery(query).uniqueResult();
		return obj;
	}
		
}
